package com.service.impl;

import com.bean.Account;
import com.dao.AccountMapper;
import com.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AccountServiceImpl implements IAccountService {
   @Autowired
   private AccountMapper accountMapper;


    @Override
    public void save(Account account) {
        // 账号名不能同名
        // 账号的默认状态为"正常"
        // 获取ip time address
        Account dbAccount = accountMapper.selectByAccount(account.getAccount());
        if(dbAccount != null){
            throw new RuntimeException("账号名已经被人占用！");
        }
        account.setStatus("正常");
        account.setLoginTime(new Date().getTime());

        accountMapper.insert(account);
    }

    @Override
    public List<Account> findall() {
        return accountMapper.selectAll();
    }

    @Override
    public Account findById(long id) {
        return accountMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(long id) {
        Account account = this.findById(id);
        if(account == null){
            throw new RuntimeException("该账号不存在");
        }
        accountMapper.deleteByPrimaryKey(id);
    }
}
